package com.camemax.dispatch.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Component;

import java.io.IOException;

// ************************
// * @Date: 2021/6/18
// * @Author: Camemax
// * @Description: Mybatis工具类
// ************************
@Component
public class MybatisUtils {
    private static final String MYBATIS_RESOURCE = "mybatis-config.xml";
    private static SqlSessionFactory sqlSessionFactory;

    static{
        try {
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(MYBATIS_RESOURCE));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 调用此方法生成SQLSession对象，获取数据库链接对象
    public SqlSession getSqlSession(){
        // 开启自动提交
        return sqlSessionFactory.openSession(true);
    }
}
